{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tools import show_subtitle\n",
    "from math import pi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# C03. 序列的一种特殊形式——字符串\n",
    "\n",
    "字符串：是不可变的序列。"
   ]
  },
  {
   "source": [
    "## 3.1 基本操作\n",
    "\n",
    "除了赋值外的其他序列操作。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "zhuyuanxiang\n"
     ]
    }
   ],
   "source": [
    "website = \"https://zhuyuanxiang.github.io\"\n",
    "print(website[8:20])\n",
    "# website[8:20]=\"yuanxiangzhu\" # Error"
   ]
  },
  {
   "source": [
    "## 3.2 设置字符串的格式：精简版\n",
    "\n",
    "1.  简化说明符\n",
    "2.  模板字符串"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "--------------- >简化说明符< ---------------\nHello, world. Hot enough for ya?\n--------------- >模板字符串< ---------------\nHello, Mars! Dusty enough for ya?\n"
     ]
    }
   ],
   "source": [
    "show_subtitle(\"简化说明符\")\n",
    "fmt_str = \"Hello, %s. %s enough for ya?\"\n",
    "values = ('world', 'Hot')\n",
    "print(fmt_str % values)\n",
    "show_subtitle(\"模板字符串\")\n",
    "from string import Template\n",
    "tmpl = Template(\"Hello, $who! $what enough for ya?\")\n",
    "print(tmpl.substitute(who=\"Mars\", what=\"Dusty\"))"
   ]
  },
  {
   "source": [
    "## 3.3 设置字符串的格式：完整版\n",
    "设置格式的信息使用一种微型格式指定语言(mini-language)。每个值都被插入到字符串中，以替换用花括号括起来的替换字段。\n",
    "\n",
    "替换字段的组成：\n",
    "\n",
    "-   字段名：索引或者标识符，指出要设置哪个值的格式并使用结果来替换该字段。除指定值外，还可以指定值的特定部分，例如：列表的元素\n",
    "-   转换标志：跟在叹号后面的单个字符。如果指定了转换标志，将不使用对象本身的格式设置机制，而是使用指定的函数将对象转换为字符串，再做进一步的格式设置。\n",
    "-   格式说明符：跟在冒号后面的表达式(使用微型格式指定语言表示的)。格式说明符能够详细地指定最终的格式，包括格式类型(例如：字符串、浮点数或者十六进制数)，字段宽度和数的精度，如何显示符号和千位分隔符，以及各种对齐和填充方式。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{ceci n'est pas une replacement field}\n{ceci n'est pas une replacement field}\n"
     ]
    }
   ],
   "source": [
    "# 使用两个 花括号 可以输出文本中显示花括号\n",
    "print(\"{{ceci n'est pas une replacement field}}\".format())\n",
    "# 使用替换的方式也可以在输出文本中显示花括号\n",
    "print(\"{}ceci n'est pas une replacement field{}\".format('{','}'))"
   ]
  },
  {
   "source": [
    "### 3.3.1 替换字段名\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "3 1 4 2\n3 2 4 1\n"
     ]
    }
   ],
   "source": [
    "    # 按顺序替换(自动编号)，指定参数名称\n",
    "    print(\"{foo} {} {bar} {}\".format(1, 2, bar=4, foo=3))\n",
    "    # 不按顺序替换(手工编号)\n",
    "    print(\"{foo} {1} {bar} {0}\".format(1, 2, bar=4, foo=3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Mr Smoketoomuch\nThe math module defines the value 3.141592653589793 for π\n"
     ]
    }
   ],
   "source": [
    "# 导入列表，使用索引访问\n",
    "fullname = ['Alfred', 'Smoketoomuch']\n",
    "print(\"Mr {name[1]}\".format(name=fullname))\n",
    "# 导入模块，使用模块的方法、属性、变量和函数\n",
    "import math\n",
    "tmpl = \"The {mod.__name__} module defines the value {mod.pi} for π\"\n",
    "print(tmpl.format(mod=math))"
   ]
  },
  {
   "source": [
    "### 3.3.2 基本转换\n",
    "\n",
    "表3-1 字符串格式设置中的类型说明符：\n",
    "-   函数 str 创建外观普通的字符串版本\n",
    "-   函数 repr 创建给定值的 Python 表示\n",
    "-   函数 ascii 创建只包含 ASCII 字符的表示"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "π 'π' '\\u03c0'\nThe number is 42\nThe number is 42.000000\nThe number is 101010\n"
     ]
    }
   ],
   "source": [
    "# 转换标志：s 代表 str；r 代表 repr；a 代表 ascii\n",
    "print(\"{pi!s} {pi!r} {pi!a}\".format(pi=\"π\"))\n",
    "# 字符 f ：表示定点数；字符 b : 表示二进制\n",
    "print(\"The number is {num}\".format(num=42))\n",
    "print(\"The number is {num:f}\".format(num=42))\n",
    "print(\"The number is {num:b}\".format(num=42))"
   ]
  },
  {
   "source": [
    "### 3.3.3 宽度\n",
    "\n",
    "设置浮点数(或者其他更加具体的小数类型)的格式时，默认在小数点后面显示 6 位小数，并且根据需要设置字段的宽度，而不进行任何形式的填充。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "-->         3          3<--\n-->Bob        Bob       <--\n"
     ]
    }
   ],
   "source": [
    "# 宽度是使用整数指定的\n",
    "print(\"-->{num:10} {num:10}<--\".format(num=3))\n",
    "print(\"-->{name:10} {name:10}<--\".format(name=\"Bob\"))"
   ]
  },
  {
   "source": [
    "### 3.3.3 精度\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0123456789012345678901234567890123456789\nPi day is 3.14\nPi day is       3.14\nGuido\n"
     ]
    }
   ],
   "source": [
    "# 精度是使用整数指定的\n",
    "print(\"0123456789012345678901234567890123456789\")\n",
    "print(\"Pi day is {pi:.2f}\".format(pi=pi))\n",
    "print(\"Pi day is {pi:10.2f}\".format(pi=pi))\n",
    "# 对字符串指定精度\n",
    "print(\"{:.5}\".format(\"Guido van Rossum\"))"
   ]
  },
  {
   "source": [
    "### 3.3.3 千位分隔符\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "One googol is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000\nOne googol is 10,000,000,000,000,000,159,028,911,097,599,180,468,360,808,563,945,281,389,781,327,557,747,838,772,170,381,060,813,469,985,856,815,104.00\nOne googol is      -3.38\n"
     ]
    }
   ],
   "source": [
    "# 使用逗号来添加 千位分隔符\n",
    "print(\"One googol is {:,}\".format(10 ** 100))\n",
    "# ToDo:使用精度显示会出现错误的结果？\n",
    "print(\"One googol is {:,.2f}\".format(10 ** 100))\n",
    "# 同时指定其他格式设置元素时，逗号应该放在表示宽度和表示精度的的符号之间\n",
    "print(\"One googol is {:10,.2f}\".format(-1.5**3))"
   ]
  },
  {
   "source": [
    "### 3.3.4 符号、对齐、使用 0 填充\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "--------------- >+、-、0、「空格」填充< ---------------\n0000003.14\n-000003.14\n     +3.14\n     -3.14\n      3.14\n     -3.14\n      3.14\n     -3.14\n"
     ]
    }
   ],
   "source": [
    "show_subtitle(\"+、-、0、「空格」填充\")\n",
    "print(\"{:010.2f}\".format(pi))\n",
    "print(\"{:010.2f}\".format(-pi))\n",
    "print(\"{:+10.2f}\".format(pi))\n",
    "print(\"{:+10.2f}\".format(-pi))\n",
    "print(\"{:-10.2f}\".format(pi))\n",
    "print(\"{:-10.2f}\".format(-pi))\n",
    "print(\"{: 10.2f}\".format(pi))\n",
    "print(\"{: 10.2f}\".format(-pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "--------------- >对齐< ---------------\n3.14000000\n0003.14000\n0000003.14\n$$$ WIN BIG $$$\n"
     ]
    }
   ],
   "source": [
    "# 指定：左对齐(<)、右对齐(>)、居中(^)\n",
    "show_subtitle(\"对齐\")\n",
    "print(\"{0:<010.2f}\\n{0:^010.2f}\\n{0:>010.2f}\".format(pi))\n",
    "print(\"{:$^15}\".format(\" WIN BIG \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "      3.14\t     -3.14\n      3.14\t-     3.14\n      3.14\t-000003.14\n      3.14\t-     3.14\n      3.14\t-     3.14\n"
     ]
    }
   ],
   "source": [
    "# 说明符(=)：将填充字符放在符号和数字之间\n",
    "print(\"{0:10.2f}\\t{1:10.2f}\".format(pi, -pi))\n",
    "print(\"{0:10.2f}\\t{1:=10.2f}\".format(pi, -pi))\n",
    "print(\"{0:10.2f}\\t{1:=010.2f}\".format(pi, -pi))\n",
    "print(\"{0:10.2f}\\t{1:=+10.2f}\".format(pi, -pi))\n",
    "print(\"{0:10.2f}\\t{1:=-10.2f}\".format(pi, -pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "3.1\t-3.1\n3.1\t-3.1\n+3.1\t-3.1\n 3.1\t-3.1\n"
     ]
    }
   ],
   "source": [
    "# 说明符：-、+、空格\n",
    "print(\"{0:.2}\\t{1:.2}\".format(pi, -pi))\n",
    "print(\"{0:-.2}\\t{1:-.2}\".format(pi, -pi))  # 默认设置\n",
    "print(\"{0:+.2}\\t{1:+.2}\".format(pi, -pi))\n",
    "print(\"{0: .2}\\t{1: .2}\".format(pi, -pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "101010\n0b101010\n52\n0o52\n2a\n0x2a\n"
     ]
    }
   ],
   "source": [
    "# 井号(#)：为数字进制转换加上前缀\n",
    "print(\"{:b}\".format(42))    # 二进制\n",
    "print(\"{:#b}\".format(42))\n",
    "print(\"{:o}\".format(42))    # 八进制\n",
    "print(\"{:#o}\".format(42))\n",
    "print(\"{:x}\".format(42))    # 十六进制\n",
    "print(\"{:#x}\".format(42))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "42\n42.0000\n"
     ]
    }
   ],
   "source": [
    "# 对于十进制数，保留小数点\n",
    "print(\"{:g}\".format(0x2a))  # 十进制\n",
    "print(\"{:#g}\".format(0x2a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "===================================\nItem                          Price\n-----------------------------------\nApples                         0.40\nPears                          0.50\nCantaloupes                    1.92\nDried Apricots (16 oz.)        8.00\nPrunes (4 lbs.)               12.00\n===================================\n"
     ]
    }
   ],
   "source": [
    "# code 3-1：字符串格式设置示例\n",
    "width = int(input(\"Please enter width: \"))\n",
    "price_width = 10\n",
    "item_width = width - price_width\n",
    "header_fmt = \"{{:{}}}{{:>{}}}\".format(item_width, price_width)\n",
    "fmt = \"{{:{}}}{{:>{}.2f}}\".format(item_width, price_width)\n",
    "print('=' * width)\n",
    "print(header_fmt.format('Item', 'Price'))\n",
    "print('-' * width)\n",
    "print(fmt.format('Apples', 0.4))\n",
    "print(fmt.format('Pears', 0.5))\n",
    "print(fmt.format('Cantaloupes', 1.92))\n",
    "print(fmt.format('Dried Apricots (16 oz.)', 8))\n",
    "print(fmt.format('Prunes (4 lbs.)', 12))\n",
    "print('=' * width)"
   ]
  },
  {
   "source": [
    "## 3.4 字符串方法\n",
    "\n",
    "string 模块中有用的常量：\n",
    "-   string.digits: 包含数字 0~9 的字符串\n",
    "-   string.ascii_letters: 包含所有 ASCII 字母(大写和小写)的字符串\n",
    "-   string.ascii_lowercase: 包含所有小写 ASCII 字母的字符串\n",
    "-   string.printable: 包含所有可以打印的 ASCII 字符的字符串\n",
    "-   string.punctuation: 包含所有 ASCII 标点字符的字符串\n",
    "-   string.ascii_uppercase: 包含所有大写 ASCII 字母的字符串\n",
    "\n",
    "注：虽然描述的是 ASCII 字符，但是值是未编码的 Unicode 字符串。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 3.4.1 center()\n",
    "\n",
    "方法通过在两边添加填充字符(默认为空格)让字符串居中"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "     The Middle byy Jimmy Eat World    \n*****The Middle byy Jimmy Eat World****\n"
     ]
    }
   ],
   "source": [
    "print(\"The Middle byy Jimmy Eat World\".center(39))\n",
    "print(\"The Middle byy Jimmy Eat World\".center(39,'*'))"
   ]
  },
  {
   "source": [
    "### 3.4.2 find()\n",
    "\n",
    "方法在字符串中查找子串。\n",
    "-   如果找到，就返回子串的第一个字符的索引\n",
    "-   如果没有找到，就返回 -1\n",
    "\n",
    "补：附录B rfind(), index(), rindex(), count(), startswith(), endswith() 用法与 find() 相同"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "moo 的位置： 7\nMonty 的位置： 0\nPython 的位置： 6\nFlying 的位置： 15\nZirquss 的位置： -1\n"
     ]
    }
   ],
   "source": [
    "print(\"moo 的位置：\",\"With a moo-moo here, and a moo-moo there\".find('moo'))\n",
    "title=\"Monty Python's Flying Circus\"\n",
    "print(\"Monty 的位置：\",title.find('Monty'))\n",
    "print(\"Python 的位置：\",title.find('Python'))\n",
    "print(\"Flying 的位置：\",title.find('Flying'))\n",
    "print(\"Zirquss 的位置：\",title.find('Zirquss'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "$$$ 的位置： 0\n指定了起点后 $$$ 的位置： 20\n!!! 的位置： 16\n指定了起点和终点后 !!! 的位置： -1\n"
     ]
    }
   ],
   "source": [
    "subject=\"$$$ Get rich now!!! $$$\"\n",
    "print(\"$$$ 的位置：\",subject.find('$$$'))\n",
    "print(\"指定了起点后 $$$ 的位置：\",subject.find('$$$',1))\n",
    "print(\"!!! 的位置：\",subject.find('!!!'))\n",
    "print(\"指定了起点和终点后 !!! 的位置：\",subject.find('!!!',1,16))"
   ]
  },
  {
   "source": [
    "### 3.4.3 join()\n",
    "\n",
    "与 split() 函数相反，用于合并序列的元素。元素必须是字符串。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "1+2+3+4+5\n"
     ]
    }
   ],
   "source": [
    "seq=[1,2,3,4,5]\n",
    "sep='+'\n",
    "# print(sep.join(seq))    # 尝试合并一个数字列表 TypeError\n",
    "seq=['1','2','3','4','5']\n",
    "print(sep.join(seq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "/usr/bin/env\nC:\\usr\\bin\\env\n"
     ]
    }
   ],
   "source": [
    "dirs='','usr','bin','env'\n",
    "print('/'.join(dirs))\n",
    "print(\"C:\"+\"\\\\\".join(dirs))"
   ]
  },
  {
   "source": [
    "### 3.4.4 lower()\n",
    "\n",
    "返回字符串的小写版本。\n",
    "\n",
    "注：\n",
    "\n",
    "1.  判断大小写的函数：islower(), istitle(), isupper(), translate()\n",
    "2.  调整大小写的函数：capitalize(), casefold(), swapcase(), title(), upper()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "trondheim hammer dance\nFound gumby!\n"
     ]
    }
   ],
   "source": [
    "print(\"Trondheim Hammer Dance\".lower())\n",
    "\n",
    "name='Gumby'\n",
    "name_list=['gumby','smith','jones']\n",
    "if name in name_list: print('Found {}!'.format(name))\n",
    "if name.lower() in name_list: print(\"Found {}!\".format(name.lower()))\n"
   ]
  },
  {
   "source": [
    "### 3.4.5 replace()\n",
    "\n",
    "将指定子串替换为另一个字符串，并且返回替换后的结果。\n",
    "\n",
    "注：\n",
    "\n",
    "1.  与替换相关的函数：translate()\n",
    "2.  附录B：expandtabs()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Theez eez a test\n"
     ]
    }
   ],
   "source": [
    "print(\"This is a test\".replace('is','eez'))"
   ]
  },
  {
   "source": [
    "### 3.4.6 split()\n",
    "\n",
    "与 join() 相反，将字符串拆分成序列。\n",
    "\n",
    "注：\n",
    "\n",
    "1.  相反的函数：join()\n",
    "2.  相关的函数：附录B：partition(), rpartition(), rsplit(), splitlines()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['1', '2', '3', '4', '5']\n['', 'usr', 'bin', 'env']\n['Using', 'the', 'default']\n"
     ]
    }
   ],
   "source": [
    "print(\"1+2+3+4+5\".split('+'))\n",
    "print(\"/usr/bin/env\".split('/'))\n",
    "print(\"Using the default\".split(' '))"
   ]
  },
  {
   "source": [
    "### 3.4.7 strip()\n",
    "\n",
    "将字符串形状和末尾的空白(不包括字符串中间的空白)删除，并且返回删除后的结果。\n",
    "\n",
    "附录B：lstrip(), rstrip()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "internal whitespace is kept\nFound gumby!\n"
     ]
    }
   ],
   "source": [
    "print('   internal whitespace is kept   '.strip())\n",
    "names=['gumby','smith','jones']\n",
    "name='gumby '\n",
    "if name in names: print(\"Found {}!\".format(name))\n",
    "if name.strip() in names: print(\"Found {}!\".format(name.strip()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "SPAM * for * everyone\nSPAM * for * everyone\n"
     ]
    }
   ],
   "source": [
    "# 删除指定的字符\n",
    "print(\"*** SPAM * for * everyone!!! ***\".strip(' *!'))\n",
    "print(\"*** SPAM * for * everyone!!! ***\".strip(' !*'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 3.4.8 translate()\n",
    "\n",
    "与 replace() 一样替换字符串的特定部分。\n",
    "\n",
    "-   replace() 一次可以对多个字符操作，但是一遍浏览只能替换一个字符串\n",
    "-   translate() 一次可以对单个字符操作，但是一遍浏览可以同时替换多个字符，效率比 replace() 高\n",
    "\n",
    "注：与之相关的函数：replace(), lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "table= {99: 107, 115: 122}\nthis is an incredible test\ntranslate string= thiz iz an inkredible tezt\ntranslate string and delete space= thizizaninkredibletezt\n"
     ]
    }
   ],
   "source": [
    "# 替换前需要创建一个转换表\n",
    "# 转换表指出了不同 Unicode 码点之间的转换关系\n",
    "# 创建转码表需要使用 str.maketrans() 方法\n",
    "# 方法的两个参数：两个长度相同的字符串，第一个字符串中的字符使用第二个字符串中的字符替换\n",
    "table=str.maketrans('cs','kz')\n",
    "print(\"table=\",table)\n",
    "string=\"this is an incredible test\"\n",
    "print(string)\n",
    "print('translate string=',string.translate(table))\n",
    "\n",
    "# 方法的第三个参数用于指定需要删除的字符\n",
    "table=str.maketrans('cs','kz',' ')\n",
    "print(\"translate string and delete space=\", string.translate(table))"
   ]
  },
  {
   "source": [
    "### 3.4.9 判断字符串是否满足特定条件\n",
    "\n",
    "使用 is 打头的函数，用于判断字符串是否具有特定的性质：\n",
    "\n",
    "-   isalnum()\n",
    "-   isalpha()\n",
    "-   isdecimal()\n",
    "-   isdigit()\n",
    "-   isidentifier()\n",
    "-   islower()\n",
    "-   isnumeric()\n",
    "-   isprintable()\n",
    "-   isspace()\n",
    "-   istitle()\n",
    "-   isupper()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.5 小结\n",
    "\n",
    "-   字符串格式设置：求模运算符(%)用于将值合并到包含转换标志的字符串，用于设置变量显示的格式\n",
    "-   字符串方法\n",
    "\n",
    "### 3.5.1 本意介绍的新函数\n",
    "\n",
    "-   string.capwords(s[,sep])：使用 split() 根据 sep 拆分 s，将每项的首字母大写，再以空格将分隔符将它们合并起来\n",
    "-   ascii(obj)：创建指定对象的 ASCII 表示"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "3.6.12-final"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}